<?php

namespace App\Models\User;

use App\Helpers\SelfEncrypt;
use App\Helpers\Traits\HandleError;
use App\Models\BaseModel;
use Frontend\Models\UserBaseModel;
use DC\UserCenter\Models\UserWalletModel;

class TfRecordModel extends BaseModel
{
    use HandleError;
    protected $table = 'tf_record';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    protected $dates = [
        'created_at',
        'updated_at',
    ];

    public function to_users()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','in_uid','member_id');
    }

    public function user()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','out_uid','member_id');
    }


    public function tfRecordIn($data)
    {
        $user_id = request()->input('user_id');
        $money = $data['money'];
        $user = UserBaseModel::find($user_id);
        if(!$user){
            $this->errorMsg = '会员信息不存在';
            return false;
        }

        switch ($data['pay_type']){
            case '201':
                $out_mid = 2 ;
                $out_field = 'money2' ;
                $in_mid = 1;
                $in_field = 'money1' ;
                break;
            case '203':
                $out_mid = 2 ;
                $out_field = 'money2' ;
                $in_mid = 3;
                $in_field = 'money3' ;
                break;
            case '204':
                $out_mid = 2 ;
                $out_field = 'money2' ;
                $in_mid = 4;
                $in_field = 'money4' ;
                break;
            default:
                $this->errorMsg = '转换类型错误';
                return false;
                break;
        }

        $tf_self_tax_rule = str2arr(json_decode(get_rule('tf_self_tax')),':');

        //计算目标币种金额
        $toMoney=( $money / $tf_self_tax_rule[0]) * $tf_self_tax_rule[1];

        $this->in_uid = $user_id;
        $this->out_uid = $user_id;
        $this->out_mid = $out_mid;
        $this->in_mid = $in_mid;
        $this->in_money = $toMoney;
        $this->out_money = $money;
        $this->service_fee = 0;
        $this->tfrate = get_rule('tf_self_tax');
        $this->op_ip =  get_client_ip(1);
        $this->comment = $data['comment'];

        if(!UserWalletModel::where('member_id',$user_id)->decrement($out_field,$money)){
            return false;
        }
        $params = [
            'money_type'=>$out_mid,
            'change_type'=>53,
            'before_money'=>$user->wallet->$out_field,
        ];
        AccountFlowModel::init()->createFlow($user_id,$money,0,$params);

        if(!UserWalletModel::where('member_id',$user_id)->increment($in_field,$toMoney)){
            return false ;
        }
        AccountFlowModel::init()->createFlow($user_id,$toMoney,1,[
            'before_money'=>$user->wallet->$in_field,
            'change_type'=>53,
            'relate_uid'=>$user_id,
            'money_type'=>$in_mid,
            'comment'=>'币种转换',
        ]);
        return $this->save();
    }


    public function tfRecordOut($data)
    {
        $user_id = request()->input('user_id');
        $money = $data['money'];
        $user = UserBaseModel::find($user_id);
        if(!$user){
            $this->errorMsg = '会员信息不存在';
            return false;
        }

        $to_number =  $data['to_number'];
        $to_user = UserBaseModel::init()->where('user_number','=',$to_number)->orWhere('user_phone','=',$to_number)->where('status','>',0)->first();
        if(!$to_user){
            $this->errorMsg = '转入账号信息不存在或未激活';
            return false;
        }

        if($user_id == $to_user['member_id']){
            $this->errorMsg = '不能自己转账给自己';
            return false;
        }

        if ($user['referrer_ids']) {

            $up_ids =UserBaseModel::init()->where('status','>=',0)->whereRaw("FIND_IN_SET(`member_id`,?)",[$user['referrer_ids']])->pluck('member_id');

            if($up_ids->isNotEmpty()){
                $up_ids = $up_ids->toArray();
            }else{
                $up_ids =[];
            }

            $down_ids = UserBaseModel::init()->where('status','>=',0)->whereRaw("FIND_IN_SET(?,referrer_ids)",$user_id)->pluck('member_id');

            if($down_ids->isNotEmpty()){
                $down_ids = $down_ids->toArray();
            }else{
                $down_ids = [];
            }

//            if (!in_array($to_user['member_id'],$down_ids) && !in_array($to_user['member_id'], $up_ids)) {
//                $this->errorMsg = '不同部门不可互转';
//                return false;
//            }
        }

        switch ($data['pay_type']){
            case '101':
                $out_mid = 1 ;
                $out_field = 'money1' ;
                $in_mid = 1;
                $in_field = 'money1' ;
                break;
            case '404':
                $out_mid = 4 ;
                $out_field = 'money4' ;
                $in_mid = 4;
                $in_field = 'money4' ;
                break;
            case '202':
                $out_mid = 2 ;
                $out_field = 'money2' ;
                $in_mid = 2;
                $in_field = 'money2' ;
                break;
            default:
                $this->errorMsg = '转账类型错误';
                return false;
                break;
        }

        $Touser = UserBaseModel::find($to_user['member_id']);

        $service_fee = 0 ;

        if(get_rule('tf_tax') >0 && get_rule('tf_tax') <100){
            $service_fee =  $money * get_rule('tf_tax') * 0.01;
        }

        $this->in_uid = $Touser['member_id'];
        $this->out_uid = $user_id;
        $this->out_mid = $out_mid;
        $this->in_mid = $in_mid;
        $this->in_money = $money -$service_fee ;
        $this->out_money = $money;
        $this->service_fee = $service_fee;
        $this->tfrate = 1;
        $this->op_ip =  get_client_ip(1);
        $this->comment = $data['comment'];

        if(!UserWalletModel::where('member_id',$user_id)->decrement($out_field,$money)){
            return false;
        }
        $params = [
            'money_type'=>$out_mid,
            'change_type'=>54,
            'before_money'=>$user->wallet->$out_field,
        ];
        AccountFlowModel::init()->createFlow($user_id,$money,0,$params);

        if(!UserWalletModel::where('member_id',$Touser['member_id'])->increment($in_field,($money-$service_fee))){
            return false ;
        }
        AccountFlowModel::init()->createFlow($Touser['member_id'],($money-$service_fee),1,[
            'before_money'=>$Touser->wallet->$in_field,
            'change_type'=>54,
            'relate_uid'=>$user_id,
            'money_type'=>$in_mid,
            'comment'=>'会员转账',
        ]);
        return $this->save();
    }

}
